import { http } from '@/js/util/connect.js';

let config = {};
let hostname = window.location.hostname;
config.getConfig = function() {
    if (hostname === 'localhost' || hostname === '127.0.0.1') {
        if (!this.data) {
            this.data = http('config.json', {}, 'GET');
        }
    } else {
        if (!this.data) {
            this.data = http(`config.${hostname}.json`, {}, 'GET');
            if (!this.data) {
                this.data = http('config.json', {}, 'GET');
            }
        }
    }
};

config.getLoginURL = function() {
    this.getConfig();
    let url = this.data.login;
    let len = url.length;
    let index = Math.floor(Math.random() * 100000) % len;
    return url[index];
};

config.getServiceURL = function(key) {
    this.getConfig();
    let port = this.data.area[0].port;
    let url = port[key];
    let len = url.length;
    let index = Math.floor(Math.random() * 100000) % len;
    return url[index];
};

config.getSystemID = function() {
    this.getConfig();
    let url = this.data.systemid;
    return url;
};

export default config;